package com.bezdelnick.forum.web.page;

import com.bezdelnick.forum.web.page.template.BaseTemplate;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.IRequestParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 07.09.12
 * Time: 12:55
 * To change this template use File | Settings | File Templates.
 */
//@At(url="/rich-text-editor")
public class TestRichTextEditorPage extends BaseTemplate {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestRichTextEditorPage.class);

    private String imageUrl = "";

    public TestRichTextEditorPage() {
        final TextArea editor = new TextArea("rich_editor", new Model(""));
        add(editor);

        final ModalWindow modalWindowUploadImage;
        add(modalWindowUploadImage = new ModalWindow("modal1"));
        modalWindowUploadImage.setCookieName("modal-1");
        modalWindowUploadImage.setPageCreator(new ModalWindow.PageCreator() {
            public Page createPage() {
                return new UploadAndInsertContentPage(TestRichTextEditorPage.this.getPageReference(), modalWindowUploadImage);
            }
        });
        modalWindowUploadImage.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
            public void onClose(AjaxRequestTarget target) {
                if (imageUrl.length() > 0) {
                    target.appendJavaScript("pasteImageUrl('" + imageUrl + "');");
                    imageUrl = "";
                }
            }
        });

        AbstractDefaultAjaxBehavior clickBehavior = new AbstractDefaultAjaxBehavior() {
            @Override
            public void renderHead(Component component, IHeaderResponse response) {
                String js = "" +
                        "function callWicket(_action) { " +
                        "   var wcall = wicketAjaxGet ('" +
                        "" + getCallbackUrl() + "&action='+_action, " +
                        "       function() { }, " +
                        "       function() { } " +
                        "   ) " +
                        "}";
                response.renderJavaScript(js, "scriptFromWicket_1");
            }

            @Override
            protected void respond(AjaxRequestTarget target) {
                IRequestParameters requestParameters = getRequestCycle().getRequest().getRequestParameters();
                String action = requestParameters.getParameterValue("action").toString();
                if (action.equals("uploadImage"))
                    modalWindowUploadImage.show(target);
            }
        };
        add(clickBehavior);
    }

    public void setImageUrl(String resourceUrl) {
        imageUrl = resourceUrl;
    }
}
